package com.kfm.util;

import java.io.Serializable;
import java.util.List;

public class Page<T> implements Serializable {

    // 当前是第几页
    private Integer current;

    // 每页几条
    private Integer pageSize;

    // 一共多少条
    private Integer total;

    // 当前页的数据
    private List<T> list;

    // 是否有上一页
    private boolean hasPre;

    // 是否有下一页
    private boolean hasNext;

    // 页码
    private int[] nums;

    public Page(Integer current, Integer pageSize, Integer total, List<T> list){
        this.current = current;
        this.pageSize = pageSize;
        this.total = total;
        this.list = list;
    }

    public Integer getCurrent() {
        return current;
    }

    public void setCurrent(Integer current) {
        this.current = current;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Integer getTotal() {
        return total;
    }

    public void setTotal(Integer total) {
        this.total = total;
    }

    public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }

    public boolean isHasPre() {
        return current != 1;
    }

    public boolean isHasNext() {
        return current < total / pageSize * 1.0;
    }

    public int[] getNums(){
        // 一共有多少页
        int num = (int)Math.ceil(total / pageSize * 1.0);
        if (num < 5){
            nums = new int[num];
            for (int i = 1; i <= num; i ++){
                nums[i - 1] = i;
            }
        } else {
            nums = new int[5];
            int c = 0; // 数组中现有的数量

            if (current <= num - 2){
                // 当前页及当前页前边的页码
                int start = current - 2 > 0?  current - 2 : 1;
                for (int i = start; i <= current; i ++){
                    nums[c ++] = i;
                }
                // 当前页后边的页码
                for(int i = current + 1; c < 5; i ++){
                    nums[c ++] = i;
                }
            } else{
                c = 5;
                // 当前页后边的页码
                for(int i = num; c > 0; i --){
                    nums[-- c] = i;
                }
            }

        }
        return nums;
    }
}
